package com.dw.controller;

import com.dw.model.message.TestMessage;
import com.dw.producer.MqProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/product")
public class MqTestController {
    @Autowired
    MqProducer mqProducer;


    /**
     * 发送同步消息
     * @return
     */
    @RequestMapping("/send/sync")
    public String sendSyncMessage() {
        TestMessage message = TestMessage.builder().msgId("1").msgContent("hello world").build();
        String str = mqProducer.sendSyncMessage("topic-test","Tag-A", message).toString();
        log.info(str);
        return "发送成功---"+str;
    }

    /**
     * 发送异步消息
     * @return
     */
    @RequestMapping("/send/async")
    public String sendAsyncMessage() {
        TestMessage message = TestMessage.builder().msgId("2").msgContent("嘻嘻哈哈").build();
        mqProducer.sendAsyncMessage("topic-test","Tag-B",message);
        return "发送成功";
    }

    /**
     * 发送一次性消息
     * @return
     */
    @RequestMapping("/send/oneway")
    public String sendOnewayMessage() {
        return "发送成功";
    }


    /**
     * 发送顺序消息
     * @return
     */
    @RequestMapping("/send/syncorderly")
    public String sendSyncOrderlyMessage() throws InterruptedException {
        TestMessage message = TestMessage.builder().msgId("1").msgContent("顺序消息").build();
        mqProducer.sendSyncOrderlyMessage("topic-orderly", message);
        return "sendSyncOrderlyMessage-顺序消息-发送完成---";
    }
}
